package com.xwh.system.controller;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

/**
 * nacos
 *
 * @author xwh
 **/
public class NacosController {

    public void updateNacosConfig() {
        String url = "http://localhost:8848/nacos/v1/cs/configs";
        String dataId = "dataId";
        String group = "DEFAULT_GROUP";
        String newContent = "newContent";

        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type", "application/x-www-form-urlencoded");

        String body = "dataId=" + dataId + "&group=" + group + "&content=" + newContent;

        HttpEntity<String> requestEntity = new HttpEntity<>(body, headers);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);

        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            System.out.println("Configuration update successful");
        } else {
            System.out.println("Configuration update failed");
        }
    }
}